<?php

namespace App\Http\Controllers;

use App\Models\CzOrder;
use App\Models\SysDept;
use App\Models\SysDictData;
use App\Models\SysDictType;
use App\Models\SysUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TestController extends Controller
{
    //
    public function getOrderList(Request $request)
    {
        // 获取待充值的号码

        return;
    }
    public function index(Request $request, SysDictType $model)
    {

        $params = $request->all();
        return error($params['i']);

        //        $data = SysDictType::filter($request->all())->with('dictDatas')->get();
        /*
        $data = SysDictData::filter($request->all())->paginate(3)->withQueryString();
//        dd($data);
        foreach ($data as $d){
            echo "{$d['id']}<br>";
        }
        return "{$data->links()}";
        */
        /*
        SysDictType::create([
            'dict_name' => '测试哈哈:'.time(),
            'dict_type' => time()
        ]);
        */
        //        $node = SysDept::whereIn('id',[100, 101, 105])->get();

        //        $result = SysDept::ancestorsAndSelf(103)->pluck('id');dd($result);
        //        $node = SysDept::find(103);
        //        dd($node->ancestors->pluck('id'));
        //        dd($node->ancestors->toArray());
        //        dd($node->descendants->toArray());

        /*
        SysDictType::find(109)->update([
            'create_by' => 8,
            'remark' => '54546'
        ]);
        */
        /*
        $user = SysUser::with('roles')->where('id',1)->first();
        $roles = $user->roles;
        dd($roles);
        return 'ok';
        */
        /*
        $list_path = base_path('channel/')."list.php";
        if(is_file($list_path)){
            $data = require_once($list_path);
        }else{
            $data = [];
        }

        return $data;
        */
        //        $this->export($request);
        //        dd($model->isFillable('create_by'));
    }
    protected function export($request)
    {


        $field = "order_sn, product_name, face_value, mobile, cost, price, created_at";
        $fileName = "客户充值订单导出[" . date('YmdHi') . "].csv";
        // 清空输出流，防止有别的信息
        ob_end_clean();
        // 打开一个输出流
        ob_start();
        // 设置header信息
        header("Content-Type: text/csv");
        header("Content-Disposition:filename=" . $fileName);

        // 打开文件流
        $fileHandle = fopen('php://output', 'w');
        //转码 防止乱码
        fwrite($fileHandle, chr(0xEF) . chr(0xBB) . chr(0xBF));
        $header_data = array(
            iconv('utf-8', 'gb2312', '订单号'),
            iconv('utf-8', 'gb2312', 'openid'),
            iconv('utf-8', 'gb2312', '充值套餐'),
            iconv('utf-8', 'gb2312', '充值号码'),
            iconv('utf-8', 'gb2312', '流量包'),
            iconv('utf-8', 'gb2312', '运营商'),
            iconv('utf-8', 'gb2312', '状态'),
            iconv('utf-8', 'gb2312', '下单时间'),
            iconv('utf-8', 'gb2312', '充值方式'),
            iconv('utf-8', 'gb2312', '成本'),
            iconv('utf-8', 'gb2312', '售价'),
            iconv('utf-8', 'gb2312', '省份'),
            iconv('utf-8', 'gb2312', '城市'),
            iconv('utf-8', 'gb2312', '上游订单号')
        );
        $header_data = [
            '订单号', '产品', '面值', '手机号', '成本', '价格', '创建时间'
        ];
        if (!empty($header_data)) {
            fputcsv($fileHandle, $header_data);
        }
        // 先把标题写进去
        //        fputcsv($fileHandle, $title);

        CzOrder::filter($request->all())->with([
            'user' => function ($query) {
                $query->select('id', 'username');
            }
        ])->orderBy('id', 'desc')->select(
            'order_sn',
            'product_name',
            'face_value',
            'mobile',
            'cost',
            'price',
            'created_at'
        )->chunk(10000, function ($items) use ($fileHandle) {
            $arrs = $items->toArray();
            foreach ($arrs as $item) {
                fputcsv($fileHandle, $item);
            }
            //每2000条释放一次资源
            ob_flush();
            flush();
        });
        //释放资源
        ob_flush();
        flush();
        ob_end_clean();
    }
    public function changeOrder()
    {
        $order =  request('order_sn');
        $res = CzOrder::whereIn('order_sn', explode(' ', $order))->where('pay_type', "huilian2")->where('order_status', 3)->update([
            "msg" => "未支付订单修正",
            "pay_status" => 0
        ]);
        return success(['data' => $res]);
    }
    public function testOrder()
    {
        // $order = ""
        // return 111;
        $order =  request('order_sn');
        // var_dump(explode(' ', $order));
        $orderInfo = CzOrder::whereIn('order_sn', explode(' ', $order))->where('pay_type', "huilian2")->where('order_status', 3)->get();
        var_dump($orderInfo->toArray());
        return success(['data' => implode(' ', $orderInfo->toArray())]);
        $order = request("order_sn");
        $pwd = request("pwd");
        if ($pwd != "qwer12344321") {
            return error('非法操作');
        }
        $order = explode(" ", $order);
        // dd($order);
        $arr = [];
        foreach ($order as $key => $val) {
            // dd($val);
            // DB::table('cz_order')
            $or = DB::table('cz_order')->where('order_sn', $val)->first();
            if ($or->order_status == 2) {
                DB::table('cz_channel_order')->where('id', $or->id)->update([
                    "channel_order_status" => 4,
                    "updated_at" => time()
                ]);
                DB::table('cz_order')->where('id', $or->id)->update([
                    "order_status" => 5,
                    "updated_at" => time()
                ]);
                $res = DB::table('cz_order_queue')->where('order_id', $or->id)->update([
                    "queue_status" => 3
                ]);
                array_push($arr, $val);
            }
        }
        return success([
            "data" => count($arr)
        ]);
    }
}
